//
//  RJDounghnutView.m
//  RJDoughnutView
//
//  Created by Po on 15/8/18.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJDounghnutView.h"
#import <CoreText/CoreText.h>

@interface RJDounghnutView ()

@property (retain, nonatomic) CAShapeLayer * dounghnutLayer;          //圆环
@property (retain, nonatomic) CATextLayer * dounghnutTextLayer;       //圆环内字
@property (retain, nonatomic) CAShapeLayer * detailLayer;             //详情

@property (assign, nonatomic) NSUInteger maxNum;                      //总和
@property (assign, nonatomic) CGFloat radio;
@property (assign, nonatomic) CGPoint centerPoint;

@property (assign, nonatomic) CGRect detailFrame;                     //详情Rect

@end

@implementation RJDounghnutView

- (void)setData:(NSArray *)data colors:(NSArray *)colors
{
    _data = [NSArray arrayWithArray:data];
    _colors = [NSArray arrayWithArray:colors];
    _maxNum = 0;
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    _radio = (width > height ? height : width);
    _centerPoint = CGPointMake(_radio, _radio);
    
    for (NSNumber * num in _data) {
        _maxNum += [num integerValue];
    }
}

- (void)setCenterPoint:(CGPoint)centerPoint radio:(CGFloat)radio
{
    _centerPoint = centerPoint;
    _radio = radio;
    _detailFrame = CGRectMake(ScreenWidth - 150,
                              _centerPoint.y - _radio,
                              100,
                              2 * _radio);
}

- (void)build:(NSUInteger)tag
{
    //圆环
    [self.layer addSublayer:[self getDounghnutLayerWithFrame:self.bounds]];
    [self.layer addSublayer:[self getDounghnutTextLayer:CGRectMake(_centerPoint.x -  _radio / 2, _centerPoint.y - _radio / 3, _radio, _radio) tag:tag]];
    [self.layer addSublayer:[self getDetailLayer:_detailFrame]];
}

- (void)buildEmpty{
     [self.layer addSublayer:[self getDounghnutLayerWithFrame:self.bounds]];
    [self.layer addSublayer:[self getDetailLayer:_detailFrame]];
}
#pragma mark - Draw
- (CAShapeLayer *)getDounghnutLayerWithFrame:(CGRect)frame
{
    _dounghnutLayer = [CAShapeLayer layer];
    
    if (_data && _data.count <= _colors.count) {
        CGFloat angleTemp = 0;
        for (NSUInteger i = 0; i < _data.count; i ++) {
            CAShapeLayer * layer = [CAShapeLayer layer];
            [layer setFrame:frame];
            [layer setLineWidth:20];
            [layer setFillColor:[UIColor clearColor].CGColor];
            
            UIColor * color = _colors[i];
            [layer setStrokeColor:color.CGColor];
            
            CGMutablePathRef path = CGPathCreateMutable();
            
            CGFloat percent = [_data[i] integerValue] / (CGFloat)_maxNum;
            CGFloat endAngleTemp = angleTemp + (2 * M_PI * percent);
            CGPathAddArc(path, NULL, _centerPoint.x , _centerPoint.y ,_radio, angleTemp, endAngleTemp, NO);
            layer.path = path;
//            CGPathRelease(path);
            //动画
            CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
            animation.duration = 1;
            animation.fromValue = @(0.0);
            animation.toValue = @(1.0);
            //    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
            [layer addAnimation:animation forKey:@"strokeEnd"];
            [_dounghnutLayer addSublayer:layer];
            angleTemp = endAngleTemp;
        }
    }
    return _dounghnutLayer;
}

- (CATextLayer *)getDounghnutTextLayer:(CGRect)frame tag:(NSUInteger)tag
{
    NSString * tittle;
    NSString * cityStr;
    if ([RJUserData defaultRJUserData].isProvince) {
        cityStr = @"全省";
    }else{
       cityStr = @"本市";
    }
    if (!_dounghnutTextLayer) {
        if (tag == 1) {
          tittle = [NSString stringWithFormat:@"全省\n空气质量分布"];
        }else if (tag == 2){
          tittle = [NSString stringWithFormat:@"%@\n城市站",cityStr];
        }else{
          tittle = [NSString stringWithFormat:@"%@\n农村站",cityStr];
        }
         NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:tittle];
        NSInteger lenght = tittle.length;
        if (tag == 1) {
            [string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)GetRGBColor(153, 153, 153, 1).CGColor range:NSMakeRange(0, lenght)];
//            [string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(10, 6)];
//            [string addAttribute:(NSString *)kCTFontAttributeName
//                           value:[UIFont systemFontOfSize:_radio / 5]
//                           range:NSMakeRange(0, 16)];
        }
        frame.size.width += 15;
        frame.origin.x -= 5;
        if (tag != 1) {
            [string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)GetRGBColor(153, 153, 153, 1).CGColor range:NSMakeRange(0, lenght)];
//            [string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor blackColor].CGColor range:NSMakeRange(0, 7)];
//            [string addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)[UIColor greenColor].CGColor range:NSMakeRange(6, 7)];
//            [string addAttribute:(NSString *)kCTFontAttributeName
//                           value:[UIFont systemFontOfSize:_radio / 5+1]
//                           range:NSMakeRange(0, 13)];
        }
        _dounghnutTextLayer = [self getTextLayerWithFrame:frame tittle:string color:nil backColor:[UIColor clearColor]];
        
    }
    return _dounghnutTextLayer;
}

- (CAShapeLayer *)getDetailLayer:(CGRect)frame
{
    if (!_detailLayer) {
        _detailLayer = [CAShapeLayer layer];
        [_detailLayer setFrame:frame];
        
        CGFloat height = frame.size.height / _data.count;
        NSArray * tittleArray = @[@"优   ",@"良   ",@"轻度 ",@"中度 ",@"重度 ",@"严重 ",@"无效 "];
        for (NSUInteger i = 0; i < _data.count; i ++) {
            CAShapeLayer * layer = [CAShapeLayer layer];
            [layer setFrame:CGRectMake(0, i * height, height - 4, height - 4)];
            [layer setCornerRadius:5];
            [layer setBackgroundColor:((UIColor *)_colors[i]).CGColor];
            NSNumber * num = @([_data[i] integerValue]);
            NSString * string = [NSString stringWithFormat:@"%@%@",tittleArray[i],num];
            
            CATextLayer * textLayer = [self getTextLayerWithFrame:CGRectMake(15 + height, i * height, frame.size.width - height, height - 4) tittle:(NSMutableAttributedString *)string color:[UIColor blackColor] backColor:[UIColor clearColor]];
            [textLayer setAlignmentMode:kCAAlignmentLeft];
            [_detailLayer addSublayer:textLayer];
            [_detailLayer addSublayer:layer];
        }
        
    }
    return _detailLayer;
}

/**公共创建方法*/
- (CATextLayer *)getTextLayerWithFrame:(CGRect)frame tittle:(NSMutableAttributedString *)tittle color:(UIColor *)color backColor:(UIColor *)backColor
{
    CATextLayer * textLayer = [CATextLayer layer];
    [textLayer setFrame:frame];
    [textLayer setFontSize:_radio / 5];
    [textLayer setString:tittle];
    [textLayer setContentsScale:2];
    [textLayer setAlignmentMode:kCAAlignmentCenter];
    [textLayer setForegroundColor:color.CGColor];
    [textLayer setBackgroundColor:backColor.CGColor];
    return textLayer;
}
@end
